package com.pacy.pacybaseproject.constviews;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.databinding.BindingAdapter;


import com.pacy.pacybaseproject.R;

import io.reactivex.annotations.Nullable;
import me.goldze.mvvmhabit.utils.StringUtils;

/**
 * @author PACY
 * @date 2021/12/1
 * description:
 */
public class Comm2TextView extends LinearLayout {
    String title;
    String content;
    int contentSize;
    int titleColor;
    int contentColor;
    TextView titleTv;
    TextView contentTt;

    public Comm2TextView(Context context) {
        super(context);
    }

    public Comm2TextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        /**
         * 加载布局  获取自定义属性
         */
        LayoutInflater.from(context).inflate(R.layout.comm2_text_view, this, true);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Comm2TextView);
        title = a.getString(R.styleable.Comm2TextView_text2Title);
        content = a.getString(R.styleable.Comm2TextView_text2Content);
        contentSize = a.getInteger(R.styleable.Comm2TextView_text2ContentSize, 16);
        titleColor = a.getColor(R.styleable.Comm2TextView_text2TitleColor, ContextCompat.getColor(getContext(), R.color.fontColor));
        contentColor = a.getColor(R.styleable.Comm2TextView_text2ContentColor, ContextCompat.getColor(getContext(), R.color.fontColor));
        a.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        contentTt = findViewById(R.id.content);
        titleTv = findViewById(R.id.title);
        setTitleTv(title);
        setContent(content);
    }

    /**
     * 设置内容
     */
    public void setContent(String content) {
        contentTt.setText(content);
        contentTt.setTextSize(contentSize);
        contentTt.setTextColor(contentColor);
    }


    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitleTv(String title) {
        titleTv.setText(title);
        titleTv.setTextColor(titleColor);
    }

    @BindingAdapter("text_content")
    public static void setStrContent(Comm2TextView commTextView, String content) {
        if (commTextView != null) {
            String mCurrentStr = commTextView.contentTt.getText().toString().trim();
            if (StringUtils.isNotEmpty(content)) {
                if (!content.equalsIgnoreCase(mCurrentStr)) {
                    commTextView.contentTt.setText(content);
                }
            }
        }
    }

    @BindingAdapter("text_title")
    public static void setStrTitle(Comm2TextView commTextView, String title) {
        if (commTextView != null) {
            String mCurrentStr = commTextView.titleTv.getText().toString().trim();
            if (StringUtils.isNotEmpty(title)) {
                if (!title.equalsIgnoreCase(mCurrentStr)) {
                    commTextView.titleTv.setText(title);
                }
            }
        }
    }

}
